package pers.zb.cloud.web.mall.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

/**
 * 订单服务
 */
@RestController
public class OrderController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/test")
    @ResponseBody
    @PreAuthorize("hasAnyAuthority('BookList')")
    public String test() {
        return "test...!";
    }

    @GetMapping("/test2")
    @ResponseBody
    @PreAuthorize("hasAnyAuthority('BookList')")
    public Object test2() {
        //Object result = restTemplate.exchange("", HttpMethod.GET,null, Map.class);
        //MultiValueMap<String, Object> goodsIdListMap = new LinkedMultiValueMap<>();
        //goodsIdListMap.add("access_token","b4a6e74d-ce8f-41d6-b7a7-f352ed2bb8c1");
        ResponseEntity<Map> response = restTemplate.exchange("http://sea-cloud-service-goods/goods/good", HttpMethod.GET, null, Map.class);
        Map map = response.getBody();
        return map;
    }

    public static HttpEntity<?> setHttpEntity(MultiValueMap<String, Object> map){

        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.add("Accept", type.toString());
        return  new HttpEntity<>(map, headers);

    }
}
